/**
 * Created by AllanXu on 7/25/2016.
 */
define(function (require) {
    var Vue = require('vue'),
        Config = require('config'),
        _ = require('ajax'),
        utilities = require('utilities'),
        userId = sessionStorage.id;
    // 认证
    var AuthDialog = require('auth_dialog');
    var authDialog = new AuthDialog();
    // 禁用申诉
    var Forbidden = require('forbidden');
    var forbidden = new Forbidden();
    var app = new Vue({
        el: '#uc_myalbum',
        data: {
            // pageIndex: 1,
            previewImg: '',
            photoWidth: 0,
            pageCount: '',
            pageno: 1,
            renderObj: {
                listArr: []
            },
            tabsName: [],
            typeId: '',
            curNum: -1,
            bottomTxt: '',
            picServer: Config.picServer
        },
        methods: {
            init: function () {
                this.photoWidth = (document.body.clientWidth - 30) / 2;
                _(Config.SERVICE.USERCENTER.MYALBUMLIST + '?id=' + userId + app.typeId + '&pageno=' + app.pageno + '&pagesize=10&type=0', {}, function (data) {
                    // _(Config.SERVICE.ACTIVITY.LIST+'?id='+4+'&param='+utilities.jsonObjToBase64(param), {}, function(data) {
                    if (app.pageno == 1) {
                        app.renderObj.listArr = data.pageitems;
                        app.pageCount = data.pagecount;
                    } else {
                        app.renderObj.listArr = app.renderObj.listArr.concat(data.pageitems);
                    }
                });
            },
            del: function (id) {
                if (confirm("确定删除？")) {
                    _(Config.SERVICE.USERCENTER.ALBUMDEL, {
                        "id": id
                    }, function (data) {
                        if (data.code == 200) {
                            app.pageno = 1;
                            app.init();
                        }
                    });
                }
            },
            setMyHead: function (id) {
                if (confirm("设为形象照？")) {
                    _(Config.SERVICE.USERCENTER.IMGSAVE, {
                        "xgmember.id": userId,
                        "id": id
                    }, function (data) {
                        if (data.code == 200) {
                            alert("设置成功~");
                        }
                    });
                }
            },
            preview: function (imgurl) {
                // var IMGURL = 'http://appapp.b0.upaiyun.com'+imgurl;
                // console.log(IMGURL);
                // // var IMGARR = app.mainInfo.zp.ann
                // _(Config.SERVICE.COMMON.WX_SIGN, {
                //     // 'openId': this.openId,
                //     'url': encodeURIComponent(location.href)
                // }, function (data) {
                //     console.log(data);
                //     wx.ready(function () {
                //         wx.previewImage({
                //             current: IMGURL, // 当前显示图片的http链接
                //             urls: [IMGURL] // 需要预览的图片http链接列表
                //         });
                //     });
                //     wx.config({
                //         debug: false,
                //         appId: data.obj.appid,
                //         timestamp: data.obj.timestamp,
                //         nonceStr: data.obj.nonceStr,
                //         signature: data.obj.signature,
                //         jsApiList: ['previewImage']
                //     });
                // });
                app.previewImg = 'http://appapp.b0.upaiyun.com' + imgurl;
            },
            closeImg: function () {
                app.previewImg = '';
            }
        }
    });
    app.init();

    window.onscroll = function () {
        var scrolltop = document.body.scrollTop | document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;
        if (scrolltop + clientHeight == document.body.clientHeight) {
            if (app.pageno <= app.pageCount) {
                app.pageno++;
                app.init();
            } else {
                app.bottomTxt = '到底儿了~';
            }
        }
    };

    //图片上传
    $('#pic').AjaxFileUpload({
        action: Config.SERVICE.USERCENTER.IMGUPLOAD,
        onComplete: function (filename, response) {
            _(Config.SERVICE.USERCENTER.IMGTOSERVER, {
                "memberAlbum.memId": userId,
                "memberAlbum.annex.id": response.title
            }, function (data) {
                if (data.code == 200) {
                    alert("上传成功~")
                    app.pageno = 1;
                    app.init();
                }
            });
        }
    });
});